'''
Created on 03.12.2009

@author: stud
'''

import vtk
import math
import time
from Tkinter import *
from vtk.tk.vtkTkRenderWidget import *

global bodyList 
global numberOfBodies
global sphereList
global counter 
global counterTwo

if __name__ == '__main__':

   
    root = Tk()
    root.title('Mehrkoerpersystem') 
    root.protocol("WM_DELETE_WINDOW", quit)
    global bodyList, numberOfBodies, counter
    bodyList = [10, 20, 
                1.4957784951205225E11, 2.580336001796161E9, 0.0,
                -2.0735999971755188E9, 2.2738241267866513E11, 0.0, 
                1.495114013741974E11,5.160289951093184E9, 0.0,
                -4.1471125609437E9, 2.2735365110227542E11, 0.0,
                1.494006688942334E11, 7.739097797219755E9, 0.0,
                -6.220362838564347E9, 2.2730571682113345E11, 0.0, 
                1.4924567869514743E11, 1.0315995730970924E10, 0.0,
                -8.293175989315513E9, 2.272386129358552E11, 0.0, 
                1.4904647072000888E11, 1.2890220421846348E10, 0.0,
                -1.0365377197431383E10, 2.2715234409765933E11, 0.0,  
                1.4880309823244174E11, 1.5461009255948238E10, 0.0,
                -1.2436791685055162E10, 2.2704691650856342E11, 0.0, 
                1.485156278126376E11, 1.802760057444985E10, 0.0, 
                -1.4507244725206564E10, 2.269223379214863E11, 0.0,
                1.4818413934887384E11, 2.058923391254348E10, 0.0,
                -1.657656165476352E10, 2.2677861764025647E11, 0.0]   

    numberOfBodies=2
    counter = numberOfBodies
    counterTwo = 0
                
    entryFrame = Frame(root)
    
    ipEntry = Entry(entryFrame)
    ipEntry.pack(side = TOP)
    portEntry = Entry(entryFrame)
    portEntry.pack(side = TOP)
    massminEntry = Entry(entryFrame)
    massminEntry.pack(side = TOP)
    massmaxEntry = Entry(entryFrame)
    massmaxEntry.pack(side = TOP)
    radiminEntry = Entry(entryFrame)
    radiminEntry.pack(side = TOP)
    radimaxEntry = Entry(entryFrame)
    radimaxEntry.pack(side = TOP)
    xEntry = Entry(entryFrame)
    xEntry.pack(side = TOP)
    yEntry = Entry(entryFrame)
    yEntry.pack(side = TOP)
    zEntry = Entry(entryFrame)
    zEntry.pack(side = TOP)
    centralStarMassEntry = Entry(entryFrame)
    centralStarMassEntry.pack(side = TOP)
    
    entryFrame.pack(side = RIGHT)

    labelFrame = Frame(root)
    
    ipLabel = Label(labelFrame, text="IP:")
    ipLabel.pack(side=TOP, ipady = 1)
    portLabel = Label(labelFrame, text="Port:")
    portLabel.pack(side= TOP, ipady = 1)
    minmassLabel = Label(labelFrame, text = "Min_Mass:")
    minmassLabel.pack(side= TOP, ipady = 1)
    maxmassLabel = Label(labelFrame, text = "Max_Mass:")
    maxmassLabel.pack(side= TOP, ipady = 1)
    radiminLabel = Label(labelFrame, text = "Min_Radius:")
    radiminLabel.pack(side= TOP, ipady = 1)
    radimaxLabel = Label(labelFrame, text = "Max_Radius:")
    radimaxLabel.pack(side= TOP, ipady = 1)
    xLabel = Label(labelFrame, text = "Ausdehnung_x:")
    xLabel.pack(side= TOP, ipady = 1)
    yLabel = Label(labelFrame, text = "Ausdehnung_y:")
    yLabel.pack(side= TOP, ipady = 1)
    zLabel = Label(labelFrame, text = "Ausdehnung_z:")
    zLabel.pack(side= TOP, ipady = 1)
    CentralStarLabel = Label(labelFrame, text = "Masse_Zentralgestirn:")
    CentralStarLabel.pack(side= TOP, ipady = 1)
    
    labelFrame.pack(side = RIGHT, expand = False)
   
   
    def setList(list):
        global bodyList
        bodyList.extend(list)
    
    ren = vtk.vtkRenderer()
    
    def setRadi():
        global sphereList
        sphereList = []
        for i in range(numberOfBodies):
            sphere = vtk.vtkSphereSource()
            sphere.SetRadius(bodyList[i])
            sphere.SetPhiResolution(20)
            sphere.SetThetaResolution(20)
            sphereMapper = vtk.vtkPolyDataMapper()
            sphereMapper.SetInputConnection(sphere.GetOutputPort())
            sphereActor = vtk.vtkActor()
            sphereActor.SetMapper(sphereMapper)
            ren.AddActor(sphereActor)
            sphereList.append(sphereActor)
        
       
    ren.SetBackground(0.1, 0.2, 0.4)
    light = vtk.vtkLight()
    light.SetPosition(- 200, - 200, 200)    # hier muss Zentralgesirn hin
    ren.AddLight(light)
    cam = ren.GetActiveCamera()
    cam.SetPosition(0, -800, 0)
    cam.SetFocalPoint(0, 0, 0)
    cam.SetViewUp(0, 0, 1)
    cam.SetViewAngle(40)
    cam.SetClippingRange(0, 1000)
    ren.SetActiveCamera(cam)
    ren.ResetCamera()
    

    renderWidget = vtkTkRenderWidget(root, width = 500, height = 500)
    ren.SetBackground(0.1, 0.2, 0.4)
    renderWindow = renderWidget.GetRenderWindow()
    renderWindow.AddRenderer(ren)
    renderWindow.SetSize(300, 300)
    #
    loopRunning = False
    exitRequested = False
    currentTime=0.0
    timeStep=0.1
  
    def quit():
        global exitRequested, loopRunning, root, vis
        if loopRunning:
            exitRequested = True    
        else:
            root.quit()
   
        
    def sphereLoop():
        setRadi()
        global loopRunning, root, exitRequested, bodyList
        loopRunnig = True
          
        while not exitRequested:
            global vis, timeStep, counter, sphereList, numberOfBodies, counterTwo
            time.sleep(timeStep)
            
            if counter >= bodyList.__len__():
                break
            sprungweite = numberOfBodies*3    
            counterTwo = 0
            for i in range(0, sprungweite, 3):
                
                positionx = bodyList[counter+i]/1000000000
                positiony = bodyList[counter+1+i]/1000000000
                positionz = bodyList[counter+2+i]/1000000000
                sphereList[counterTwo].SetPosition(positionx, positiony, positionz)
                counterTwo += 1
             
            counter += numberOfBodies*3   
            renderWindow.Render()
            root.update()
        
#        root.quit()
            
            
    
                  
    #
    renderWidget.pack(expand = 'true', fill = 'both')      
    #
    startButton = Button(root, text = 'Start', command = sphereLoop)
    startButton.pack(expand = 'true', fill = 'x')
    quitButton = Button(root, text = "Quit", command = quit)
    quitButton.pack(expand = 'true', fill = 'x')
    
    #
    root.mainloop()
